<template>
  <div class="forget_paw">
    <div class="title">忘记密码</div>
    <div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="姓名:" prop="userName">
          <el-input
            v-model="ruleForm.userName"
            placeholder="请输入姓名"
          ></el-input>
        </el-form-item>
        <el-form-item label="身份证号码:" prop="idCard">
          <el-input
            v-model="ruleForm.idCard"
            placeholder="请输入身份证号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="newPassWord">
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="ruleForm.newPassWord"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码:" prop="checkPass">
          <el-input
            placeholder="请再次输入密码"
            type="password"
            v-model="ruleForm.checkPass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" @click="submitForm('ruleForm')">提 交</el-button>
  </div>
</template>

<script>
import { forgetPassWord } from "@/utils/apis";
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newPassWord) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        newPassWord: "",
        checkPass: "",
        userName: "",
        idCard: "",
      },
      rules: {
        newPassWord: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
        userName: [
          {
            required: true,
            message: "请输入姓名",
            trigger: "blur",
          },
        ],
        idCard: [
          {
            required: true,
            message: "请输入身份证号码",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          forgetPassWord(this.ruleForm).then((res) => {
            console.log(res);
            this.$message.success("修改成功!");
            this.$refs.ruleForm.resetFields();
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>
<style  lang="scss" scope>
.forget_paw {
  background-color: #fff;
  border-radius: 6px;
  width: 500px;
  padding: 30px;
  margin: 0 auto;
  height: auto;
  text-align: center;
  .title {
    font-size: 26px;
    margin-bottom: 30px;
  }
}
</style>
